<template>
  <div class="love-talk">
    <ul class="talk-list">
      <li v-for="talk in loveTalkStore.loveTalk" :key="talk.id" class="talk-item">{{ talk.content }}</li>
    </ul>
    <button @click="getTalks" class="talk-button">获取土味情话</button>
  </div>
</template>

<script lang="ts">
export default {
  name: "LoveTalk",
}
</script>

<script setup lang="ts">
import {useLoveTalkStore} from "../store/LoveTalk"

const loveTalkStore = useLoveTalkStore();
function getTalks() {
  loveTalkStore.getTalk()
}
//相当于watch,mutation：描述状态变更的对象，state：状态对象
// 监听数据,监听数据的变化，并通过存储新数据state中的loveTalk集合属性到浏览器的缓存中，实现刷新页面不丢失的功能
loveTalkStore.$subscribe((mutation, state) => {

  console.log("打印mutation+state，看看里面有什么", mutation, state)
  //序列化
  localStorage.setItem("loveTalk", JSON.stringify(state.loveTalk))//将数组对象转换为JSON字符串存储（必须）存储到浏览器缓存中：浏览器右键检查的application中查看本地存储
})
</script>

<style scoped>
.love-talk {
  background-color: #f0f8ff;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  max-width: 500px;
  margin: 20px auto;
}

.talk-list {
  list-style-type: none;
  padding: 0;
  margin-bottom: 20px;
}

.talk-item {
  background-color: #ffffff;
  border: 1px solid #ddd;
  border-radius: 5px;
  padding: 10px 15px;
  margin-bottom: 10px;
  font-size: 16px;
  color: #333;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.talk-item:hover {
  background-color: #e6f7ff;
  transform: translateY(-2px);
  transition: all 0.3s ease;
}

.talk-button {
  background-color: #409eff;
  color: white;
  border: none;
  border-radius: 5px;
  padding: 10px 20px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.talk-button:hover {
  background-color: #66b1ff;
  transform: scale(1.05);
  transition: all 0.3s ease;
}

.talk-button:active {
  transform: scale(0.95);
}
</style>